package com.coursebooking.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coursebooking.model.Booking;
import com.coursebooking.model.Course;
import com.coursebooking.model.Material;
import com.coursebooking.model.Review;
import com.coursebooking.model.Teacher;
import com.coursebooking.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class CourseService extends ServiceImpl<CourseRepository, Course> {

    @Autowired
    private CourseRepository courseRepository;


    @Autowired
    private BookingRepository bookingRepository;
    
    @Autowired
    private MaterialRepository materialRepository;
    
    @Autowired
    private ReviewRepository reviewRepository;
    @Autowired
    private TeacherRepository teacherRepository;


    public Optional<Course> findById(Long id) {
        Optional<Course> course = Optional.ofNullable(courseRepository.selectById(id));
        course.ifPresent(value -> format(Collections.singletonList(value)));
        return course;
    }

    public Optional<Course> findByCode(String code) {
        return courseRepository.findByCode(code);
    }

    public List<Course> findAll() {
        List<Course> courses = courseRepository.selectList(null);
        format( courses);
        return courses;
    }

    public void format(List<Course> courses) {
        for (Course cours : courses) {
            if (cours.getTeacherId() != null){
                Teacher teacher = teacherRepository.selectById(cours.getTeacherId());

                cours.setTeacher(teacher);
            }
        }

    }
    public List<Course> findByTeacher(Teacher teacher) {
        List<Course> byTeacher = courseRepository.findByTeacher(teacher);
        format(byTeacher);
        return byTeacher;
    }

    public List<Course> findByStatus(Course.CourseStatus status) {
        List<Course> byStatus = courseRepository.findByStatus(status);
        format(byStatus);
        return byStatus;
    }

    public List<Course> findByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        List<Course> byTimeRange = courseRepository.findByTimeRange(startTime, endTime);
        format(byTimeRange);
        return byTimeRange;
    }

    public List<Course> findByKeyword(String keyword) {
        return courseRepository.findByKeyword(keyword);
    }

    public Course updateCourse(Course course) {
        if (courseRepository.selectById(course.getId()) == null) {
            throw new RuntimeException("课程不存在");
        }
        courseRepository.updateById(course);
        return course;
    }

    public void deleteCourse(Long id) {
        Course course = courseRepository.selectById(id);
        if (course == null) {
            throw new RuntimeException("课程不存在");
        }
        
        // 删除相关预约
        List<Booking> bookings = bookingRepository.findByCourse(course);
        for (Booking booking : bookings) {
            bookingRepository.deleteById(booking.getId());
        }
        
        // 删除相关材料
        List<Material> materials = materialRepository.findByCourse(course);
        for (Material material : materials) {
            materialRepository.deleteById(material.getId());
        }
        
        // 删除相关评价
        List<Review> reviews = reviewRepository.findByCourse(course);
        for (Review review : reviews) {
            reviewRepository.deleteById(review.getId());
        }
        
        courseRepository.deleteById(id);
    }

    public boolean existsByCode(String code) {
        return courseRepository.existsByCode(code);
    }

    public boolean existsById(Long id) {
        return courseRepository.selectById(id) != null;
    }

    public Course updateCourseStatus(Long courseId, Course.CourseStatus status) {
        Course course = courseRepository.selectById(courseId);
        if (course != null) {
            course.setStatus(status);
            courseRepository.updateById(course);
            return course;
        } else {
            throw new RuntimeException("课程不存在");
        }
    }
}